import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import TakePhotoView from '../views/TakePhotoView.vue'
import ChatDesView from '../views/ChatDesView.vue'
import MapView from '../views/MapView.vue'
import AddressView from '../views/AddressView.vue'
import DiscoverView from '../views/DiscoverView.vue'
import CircleOfFriendsView from '../views/CircleOfFriendsView.vue'
import PublishTextView from '../views/PublishTextView.vue'
import MyView from '../views/My/MyView.vue'
import InitPage from '../views/InitPage/InitPage.vue'
import LoginPage from '../views/InitPage/LoginPage.vue'
import RegisterPage from '../views/InitPage/RegisterPage.vue'
import addFriendsView from '../views/addFriends/addFriendsView.vue'
import SearchView from '../views/addFriends/SearchView.vue'
import SearchAll from '../views/search/SearchAll.vue'
import ApplyView from '../views/addFriends/ApplyView.vue'
import MsgFromFriends from '../views/addFriends/MsgFromFriends.vue'
import WantToChat from '../views/WantToChat.vue'
import BasicMessage from '../views/addFriends/BasicMessage.vue'

import cookie from "vue-cookies"
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
  },
  {
    path: '/chatdes',
    name: 'chatdes',
    component: ChatDesView,
    meta: {
      showTabbar: true,
    }
  },
  {
    path: '/take',
    name: 'take',
    component: TakePhotoView,
    meta: {
      showTabbar: true,
    }
  },
  {
    path: '/map',
    name: 'map',
    component: MapView,
    meta: {
      showTabbar: true,
    }
  },
  {
    path: '/address',
    name: 'address',
    component: AddressView,
  },
  {
    path: '/discover',
    name: 'discover',
    component: DiscoverView,
  },
  {
    path: '/circle',
    name: 'circle',
    component: CircleOfFriendsView,
    meta: {
      showTabbar: true,
    }
  },
  {
    path: '/publish',
    name: 'publish',
    component: PublishTextView,
    meta: {
      showTabbar: true,
    }
  },

  {
    path: '/my',
    name: 'my',
    component: MyView,
  },
  {
    path: '/init',
    name: 'Init',
    component: InitPage,
    meta: {
      showTabbar: true,
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: LoginPage,
    meta: {
      showTabbar: true,
    }
  }, {
    path: '/register',
    name: 'Register',
    component: RegisterPage,
    meta: {
      showTabbar: true,
    }
  },
  {
    path: '/addFriends',
    name: 'addFriends',
    component: addFriendsView,
    meta: {
      showTabbar: true,
    }
  },
  {
    path: '/searchView',
    name: 'searchView',
    component: SearchView,
    meta: {
      showTabbar: true,
    }
  },
  {
    path: '/my/setup',
    name: 'setUp',
    component: () => import("../views/My/SetUpView.vue"),
    meta: {
      showTabbar: true,
    }
  },
  {
    path: '/my/setup/switchtouser',
    name: 'switchToUser',
    component: () => import("../views/My/SwitchToUserPage.vue"),
    meta: {
      showTabbar: true,
    }
  },
  {
    path: '/searchAll',
    name: 'searchAll',
    component: SearchAll,
    meta: {
      showTabbar: true,
    }
  },
  {
    path: '/applyView',
    name: 'applyView',
    component: ApplyView,
    meta: {
      showTabbar: true,
    }
  },
  {
    path: '/msgFromFridens',
    name: 'msgFromFriends',
    component: MsgFromFriends,
    meta: {
      showTabbar: true,
    }
  },
  {
    path: '/basicMessage',
    name: 'basicMessage',
    component: BasicMessage,
    meta: {
      showTabbar: true,
    }
  },

  {
    path: '/wantToChat/:list',
    name: 'wantToChat',
    component: WantToChat,
    meta: {
      showTabbar: true,
    }
  },
  {
    path: '/scan',
    name: 'scan',
    component: () => import('../views/scanPage/ScanView.vue')
  },
  {
    path: '/video',
    name: 'video',
    component: () => import('../views/videoNumber/VideoView.vue')
  },
  {
    path: '/videonew',
    name: 'videonew',
    component: () => import('../views/videoNumber/VideoNew.vue')
  },
  {
    path: '/my/userinfo',
    name: 'userInfo',
    component: () => import('../views/My/UserInfo.vue'),
    meta: {
      showTabbar: true,
    }
  },
  {
    path: '/check',
    name: 'check',
    component: () => import('../views/service/CheckView.vue')
  },
  {
    path: '/buildgroupchatpage',
    name: 'buildGroupChatPage',
    component: () => import('../views/newGroupChat/BuildGroupChatPage.vue'),
    meta: {
      showTabbar: true,
    }
  },
  {
    path: '/newgroupchatpage',
    name: 'newGroupChatPage',
    component: () => import('../views/newGroupChat/NewGroupChatPage.vue'),
    meta: {
      showTabbar: true,
    }
  },

  {
    path: '/groupmessage',
    name: 'groupmessage',
    component: () => import('../views/GroupAddition/GroupMessage.vue')
  },
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})  


// 每次创建新的页面, 记得完数据中添加一个 router的name 名
const needLoginRoute = ['home', 'buildGroupChatPage', 'userInfo', 'address', 'discover', 'my', 'searchView', 'addFriends', 'sacan', 'video', "setUp", "switchToUser"]
// 全局路由守卫
router.beforeEach((to, from, next) => {
  if (cookie.get('wxUserToken') !== null) {
    next()
  } else {
    let index = needLoginRoute.findIndex(n => n == to.name)
    if (index != -1) { // 此路由需登录才可以访问
      // ...
      // 未登录 重定向到登录页面
      next({ path: '/init' })
    }
    next()
  }
})
export default router
